package com.dm.library.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加密工具类
 *
 * @author jiaohongyun
 * @date 2015年6月5日
 */
public class EncryptUtil {
    private static final String IV_STRING = "8532164579245368";

    public static final String SIGN = "sign";

    /**
     * 加密
     *
     * @param cleartext
     * @param dataPassword
     * @return
     */
    public static String encrypt(String cleartext, String dataPassword) {
        byte[] encryptedData = null;
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(IV_STRING.getBytes("utf-8"));
            SecretKeySpec key = new SecretKeySpec(cleartext.getBytes("utf-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            encryptedData = cipher.doFinal(dataPassword.getBytes("utf-8"));
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return Base64.encodeToString(encryptedData, Base64.NO_WRAP);
    }

    /**
     * 解密
     *
     * @param dataPassword
     * @param encrypted
     * @return
     */
    public static String decrypt(String encrypted,String dataPassword) {
        byte[] decryptedData = null;
        String res = null;
        try {
            byte[] byteMi = Base64.decode(dataPassword, Base64.NO_WRAP);
            IvParameterSpec zeroIv = new IvParameterSpec(IV_STRING.getBytes("utf-8"));
            SecretKeySpec key = new SecretKeySpec(encrypted.getBytes("utf-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            decryptedData = cipher.doFinal(byteMi);
            res = new String(decryptedData, "utf-8");
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return res;
    }

    /**
     * Base64加密
     * @param data
     * @param key
     * @return
     */
    public static String base64Encrypt(String data, String key) {
        if (data == null) {
            return "";
        }
        String encode = "";
        try {
            encode = new String(Base64.encode(data.getBytes("utf-8"), Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        encode += key;
        byte[] b = encode.getBytes();
        for (int i = 0; i < b.length / 2; i++) {
            byte temp = b[i];
            b[i] = b[b.length - 1 - i];
            b[b.length - 1 - i] = temp;
        }
        return new String(b);
    }

    /**
     * Base64解密
     * @param data
     * @param key
     * @return
     */
    public static String base64Decode(String data, String key) {
        if (data == null) {
            return "";
        }
        String decode = "";
        return decode;
    }

    /**
     * 加密登录密码
     * @param src
     * @return
     */
    public static String encryptLoginPwd(String src){
        return new String(Base64.encode(toMD5(src).getBytes(), Base64.NO_WRAP));
    }

    /***
     * MD5加密
     * @param src
     * @return
     */
    public static String toMD5(String src) {
        try {
            //生成实现指定摘要算法的 MessageDigest 对象。
            MessageDigest md = MessageDigest.getInstance("MD5");
            //使用指定的字节数组更新摘要。
            md.update(src.getBytes());
            //通过执行诸如填充之类的最终操作完成哈希计算。
            byte b[] = md.digest();
            //生成具体的md5密码到buf数组
            int i;
            StringBuilder buf = new StringBuilder("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            //            System.out.println("32位: " + buf.toString());// 32位的加密
            //            System.out.println("16位: " + buf.toString().substring(8, 24));// 16位的加密，其实就是32位加密后的截取
            return buf.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return src;
    }
}